package data.visu.utils;

import java.awt.Color;
import java.util.HashMap;
import java.util.Iterator;

import data.visu.structures.ValueAndString;


/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class ColorConvertor {


	public static Color getOppositeColor(Color color){
		//Cas niveaux de gris
		if(Math.abs(color.getRed()-color.getGreen())<15
				&& Math.abs(color.getRed()-color.getBlue())<15){
			int maxValue = Math.max(color.getRed(), Math.max(color.getGreen(), color.getBlue()));
			int newValue;
			if(maxValue>128)newValue=0;
			else newValue=255;
			Color res = new Color(newValue,newValue,newValue);
			return res;
		}
		else{
			float[]hsbVals = new float[3];
			Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsbVals);

			float hue = hsbVals[0];
			float saturation = hsbVals[1];
			float value = hsbVals[2];
			hue=(hue+0.5f)%1.0f;
			Color res = new Color(Color.HSBtoRGB(hue, saturation, value));
			return res;
		}

	}
	
	
	public static void makeColorShaderForBand(HashMap<ValueAndString,Color> map, Color color){
		int nbColor = map.size();
		int r = color.getRed();
		int g = color.getGreen();
		int b = color.getBlue();
		int step;
		if(nbColor>1)step=170/(nbColor-1);
		else step=170;
		int alpha;
		Iterator<ValueAndString> iter = map.keySet().iterator();
		while(iter.hasNext()){
			ValueAndString vs = iter.next();
			alpha = (int) (180-(vs.getValue()*step));
			map.put(vs,new Color(r,g,b,alpha));
		}
	}
}
